let Controller = {
  mounted() {
    this.initData();
  },
  activated() {
    this.initData();
    this.requestDetail();
  },
  methods: {
    initData() {
      const param = JSON.parse(this.$route.query.data);
      this.Model.queryData = _.cloneDeep(param);
    },
    onApproval() {
      const orderId = this.Model.queryData.id;
      const params = [orderId];
      if (params && params.length > 0) {
        this.$confirm('是否确认审核?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(() => {
          this.$ajax
            .post(this.apiTest + `/sale/onSale/order/sale/approval`, params)
            .then(response => {
              const data = response.data;
              if (data.status == 200 && data.result == 1) {
                this.$message({
                  type: 'success',
                  message: '已成功审核'
                });
                this.requestDetail();
              } else {
                this.$message({
                  type: 'error',
                  message: data.msg
                });
              }
            })
            .catch(error => {
              this.$message.error('请求错误');
            });
        }).catch(() => {

        });
      }
    },
    tableDetail(row) {
      this.Model.detailDialogFlag = true;
    },
    deleteOrderGifts(id) {
      this.$confirm('是否确认删除赠品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        this.$ajax.delete(this.apiTest + `/sale/saleOrderGifts/${id}`, {})
          .then(response => {
            const data = response.data;
            if (data.status == 200) {
              this.$message.success('删除成功');
              this.requestDetail();
            }
          }).catch(err => {

          })
      })

    },
    requestDetail() {
      const orderId = this.Model.queryData.id;
      if (orderId) this.$ajax
        .get(this.apiTest + `/sale/onSale/order/getRefundDetail/${orderId}`)
        .then(response => {
          const data = response.data;
          const result = data.result;
          this.Model.idCard = result.idCard;
          this.Model.saleOrderGiftsList = result.saleOrderGiftsList;
          this.Model.saleOrderPayList = result.saleOrderPayList;
          this.Model.refundList = result.refundList;
          this.Model.saleLogList = result.saleLogList;
          this.Model.tableData.returnGoodsInfo = _.cloneDeep([result.returnGoodsInfo]);
          this.Model.tableData.list = _.cloneDeep(result.list);
					this.Model.aftersaleStatus = result.aftersaleStatus;
          this.setStepData(result.saleOrderStatusList, result.aftersaleStatus);
					this.Model.tabFormInline = [{
					    label: '售中单号',
					    value: this.Model.queryData.saleNumber,
					  },
					  {
					    label: '售后状态',
					    value: data.result.aftersaleStatusName
					  },
					  {
					    label: 'sap售中单号',
					    value: this.Model.queryData.sapSaleNumber
					  },
					  {
					    label: '原销售定单号',
					    value: this.Model.queryData.originNumber
					  },
					  {
					    label: '原sap销售订单号',
					    value: this.Model.queryData.originSapNumber
					  }
					];
          this.Model.tableFormInline = [{
              label: '订单编号',
              value: result.orderNumber || '',
            },
            {
              label: '原sap销售订单号',
              value: result.sapOrderNumber || '',
            },
            {
              label: '下单人',
              value: result.orderBuyer || '',
            },
            {
              label: '配送类型',
              value: result.deliveryType || '',
            },
            {
              label: '发货平台',
              value: result.shipperStore || '',
            },
            {
              label: '销售来源',
              value: result.saleOrigin || '',
            },
            {
              label: '销售员',
              value: result.saleMan || '',
            }
          ];
        })
        .catch(error => {
          this.$message.error('请求错误');
        });
    },
    setStepData(list, status) {
      let activeStep = 0,
        step = [];
      for (let i = 0; i < list.length; i++) {
        if (list[i].code == status) activeStep = i;
        step.push({
          title: list[i].codeName,
          description: list[i].createTime
        })
      };
      this.Model.refundStep = {
        activeStep,
        step
      }
    },
    warehousing(row) {
      this.Model.warehousingDialogStatus = row.status;
      this.Model.warehousingDialogFlag = true;
    },
    handleSelectionChange(val) {},
    handleCurrentChange(val) {
      this.Model.pager.params.pageIndex = val;
    },
    handleSizeChange(val) {
      this.Model.pager.params.pageSize = val;
    },
    cardClick(color) {
      this.Model.cardSelected = color;
    },
    showChild(val) {
      this.Model.searchFromData = val;
    },
    toSearch(val) {},
    toClear(val) {
      this.Model.searchFromData = this.Model.defaultSearchFromData;
    },
    toExport(val) {},
    onTabChange(val) {
      switch (val) {
        case 1:
          break;
        case 2:
          break;
        case 3:
          break;
      }
    },
    toggleTableDetail(val) {
      const obj = this.Model[val];
      if (obj.flag === false) {
        this.Model[val].src = require('@/assets/img/icon_collapse.png');
        this.Model[val].flag = true;
      } else {
        this.Model[val].src = require('@/assets/img/icon_unfold.png');
        this.Model[val].flag = false;
      }
    },
    onDialogEdit(flag, val) {
      this.Model.editDialogFlag = flag;
      if (flag === false) return;
      const obj = this.Model[val];
      this.Model.editDialogFormData = _.cloneDeep(this.Model.defaultEditDialogFormData);
      this.getAddress();
    },
    changeAddress(val) {
      if (!val) {
        this.getAddress();
      }
    },
    getAddress() {
      const idCard = this.Model.idCard;
      this.$ajax.post(this.apiTest + `/sale/saleOrder/getAddressByIdCard/${idCard}`, {})
        .then(response => {
          const data = response.data;
          if (data.status == 200) {
            this.Model.addressList = data.result;
          }
        }).catch(err => {})
    },
    updateBaseSaleOrder() {
      const orderId = this.Model.queryData.id;
      const data = {
        ...this.Model.editDialogFormData,
        orderId,
        idCard: this.Model.idCard,
      };
      let params;
      if (data.shippingMethod === '1') {
        params = {
          orderId,
          idCard: this.Model.idCard,
          shippingMethod: data.shippingMethod,
          shipper: data.shipper,
          originChannel: data.originChannel,
          salesman: data.salesman,
        }
      } else {
        if (this.Model.editDialogNewAddress == false) {
          const index = this.Model.editDialogItemAddressBoxRadio;
          const list = this.Model.addressList;
          data.recipientName = list[index].name;
          data.recipientPhone = list[index].phone;
          data.detailedAddress = list[index].address;
        }
        params = data;
      }
      this.$ajax.post(this.apiTest + `/sale/saleOrder/updateBaseSaleOrder`, params)
        .then(response => {
          const data = response.data;
          if (data.status == 200) {
            this.requestDetail();
            this.onDialogEdit(false);
            this.$message({
              type: 'success',
              message: '已成功保存'
            });

          } else {
            this.$message.error(data.msg);
          }
        }).catch(err => {
          this.$message.error(err);
          this.onDialogEdit(false);
        });
    },
    getStatusText(type, value) {
      let text = '';
      switch (type) {
        case 'aftersaleStatus':
          /* 售后：0退款中/1退款成功/2退款拒绝/ 售中：3待审核/4已取消/5变更成功/6待用户支付/7待平台退款 */
          switch (value) {
            case 0:
              text = '退款中';
              break;
            case 1:
              text = '退款成功';
              break;
            case 2:
              text = '退款拒绝';
              break;
            case 3:
              text = '待审核';
              break;
            case 4:
              text = '已取消';
              break;
            case 5:
              text = '变更成功';
              break;
            case 6:
              text = '待用户支付';
              break;
            case 7:
              text = '待平台退款';
              break;
            default:
              text = '暂无';
              break;
          }
          break;
        default:
          break;
      }
      return text;
    },
  }
}
export default Controller;
